package cn.yunbamboos.rest.service.core;

import cn.yunbamboos.rest.service.IBeanInvoke;
import cn.yunbamboos.rest.service.ICreateInDTO;
import cn.yunbamboos.rest.service.IRestService;
import cn.yunbamboos.rest.service.dto.in.BaseInDTO;
import cn.yunbamboos.rest.service.dto.out.BaseOutDTO;
import cn.yunbamboos.rest.service.filter.RestServiceFilter;
import cn.yunbamboos.rest.service.transaction.Transaction;

public class RestService implements IRestService {
    /**
     * 服务访问路径
     */
    private String url;
    /**
     * 服务名称
     */
    private String name;
    /**
     * 是否需要认证
     */
    private boolean authentication;
    /**
     * 是否记录调用日志
     */
    private boolean log;
    /**
     * 事务类型
     */
    private Transaction transaction;
    /**
     * 前置过滤器
     * */
    private Class<? extends RestServiceFilter>[] filters;
    /**
     * 后置过滤器
     * */
    private Class<? extends RestServiceFilter>[] afterFilters;
    /**
     * 入参类型
     */
    private Class<? extends BaseInDTO> in;
    /**
     * 出参类型
     */
    private Class<? extends BaseOutDTO> out;

    private ICreateInDTO createInDTO;
    private IBeanInvoke invoke;

    @Override
    public String url() {
        return url;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public boolean authentication() {
        return authentication;
    }

    @Override
    public boolean log() {
        return log;
    }

    @Override
    public Transaction transaction() {
        return transaction;
    }

    @Override
    public Class<? extends RestServiceFilter>[] filters(){
        return filters;
    }

    @Override
    public Class<? extends RestServiceFilter>[] afterFilters(){
        return afterFilters;
    }

    @Override
    public Class<? extends BaseInDTO> in() {
        return in;
    }

    @Override
    public Class<? extends BaseOutDTO> out() {
        return out;
    }

    @Override
    public ICreateInDTO createInDTO(){
        return createInDTO;
    }

    @Override
    public IBeanInvoke invoke(){
        return invoke;
    }

    public static class RestServiceBuilder implements IRestService.Builder {

        private final RestService service = new RestService();

        @Override
        public Builder url(String url) {
            service.url = url;
            return this;
        }

        @Override
        public Builder name(String name) {
            service.name = name;
            return this;
        }

        @Override
        public Builder authentication(boolean authentication) {
            service.authentication = authentication;
            return this;
        }

        @Override
        public Builder log(boolean log) {
            service.log = log;
            return this;
        }

        @Override
        public Builder transaction(Transaction transaction) {
            service.transaction = transaction;
            return this;
        }

        @Override
        public Builder filters(Class<? extends RestServiceFilter>[] filters) {
            service.filters = filters;
            return this;
        }

        @Override
        public Builder afterFilters(Class<? extends RestServiceFilter>[] filters) {
            service.afterFilters = filters;
            return this;
        }

        @Override
        public Builder in(Class<? extends BaseInDTO> in) {
            service.in = in;
            return this;
        }

        @Override
        public Builder out(Class<? extends BaseOutDTO> out) {
            service.out = out;
            return this;
        }

        @Override
        public Builder createInDTO(ICreateInDTO createInDTO){
            service.createInDTO = createInDTO;
            return this;
        }

        @Override
        public Builder invoke(IBeanInvoke beanInvoke){
            service.invoke = beanInvoke;
            return this;
        }

        @Override
        public IRestService build() {
            return service;
        }
    }
}
